import { createPinia } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate'

// 导入所有store模块
export { useAuthStore } from './modules/auth'
export { useCounterStore } from './modules/counter'
export { useUserStore } from './modules/user'
export { useAppStore } from './modules/app'
export { useResourceStore } from './modules/resource'

// 创建pinia实例并配置持久化插件
export const pinia = createPinia()
pinia.use(createPersistedState({
  // 全局配置
  storage: localStorage, // 使用 localStorage 存储
  key: id => `pinia-${id}`, // 存储键名前缀
  auto: true, // 自动持久化所有 store
}))

// 导出类型
export type { UserInfo } from './modules/auth'
export type { UserProfile } from './modules/user'
export type { AppConfig } from './modules/app'

// 初始化所有store的方法
export const initStores = () => {
  // 使用ES6 import动态导入
  import('./modules/auth').then(({ useAuthStore }) => {
    const authStore = useAuthStore()
    authStore.initAuth()
  })
  
  import('./modules/user').then(({ useUserStore }) => {
    const userStore = useUserStore()
    userStore.initUser()
  })
  
  import('./modules/app').then(({ useAppStore }) => {
    const appStore = useAppStore()
    appStore.initApp()
  })
  
  import('./modules/counter').then(({ useCounterStore }) => {
    const counterStore = useCounterStore()
    counterStore.initCounter()
  })

  import('./modules/resource').then(({ useResourceStore }) => {
    const resourceStore = useResourceStore()
    resourceStore.initResource()
  })
}